<template>
  <div>
    <el-dialog
      :title="info.isAdd ? '管理员添加' : '管理员编辑'"
      :visible.sync="info.isshow"
      @closed="close"
    >
      <el-form :model="form">
        {{ form }}
        <el-form-item label="所属角色" :label-width="formLabelWidth">
          <el-select v-model="form.roleid">
            <el-option disabled value="" label="请选择"></el-option>
            <el-option
              v-for="item in roleList"
              :key="item.id"
              :label="item.rolename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户名" :label-width="formLabelWidth">
          <el-input v-model="form.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" :label-width="formLabelWidth">
          <el-input v-model="form.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="状态" label-width="120px">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="info.isshow = false">取 消</el-button>
        <el-button type="primary" @click="add" v-if="info.isAdd"
          >添 加</el-button>
        <el-button type="primary" @click="xiugai" v-else>修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import {
  reqrolelist,
  requseradd,
  requserinfo,
  requseredit,
  reqLogin,
} from "../../../request/api";
import { successAlert } from "../../../utils/alert";
export default {
  props: ["info"],
  data() {
    return {
      isshow: false,
      roleList: [],
      form: {
        roleid: "",
        username: "",
        password: "",
        status: 1,
      },
      formLabelWidth: "120px",
    };
  },
  computed: {
    ...mapGetters({}),
  },
  mounted() {
    //请求角色数据
    reqrolelist().then((res) => {
      if (res.data.code == 200) {
        this.roleList = res.data.list ? res.data.list : [];
        // console.log(this.roleList);
      }
    });
  },
  methods: {
    //清空数据
    qingkong() {
      this.form = {
        roleid: "",
        username: "",
        password: "",
        status: 1,
      };
    },
    ...mapActions({
      changeuser:"changeuser"
    }),
    add() {
      requseradd(this.form).then((res) => {
        if (res.data.code == 200) {
          // 1.弹成功
          successAlert(res.data.msg);
          // 2.清空form
          this.qingkong();
          // 3.弹框消失
          this.info.isshow = false;
          // 4.列表重新请求
          this.$emit("init");
        }
      });
    },
    //获取一条数据
    getOne(id) {
      //取数据
      requserinfo({ uid: id }).then((res) => {
        if (res.data.code == 200) {
          this.form = res.data.list;
          this.form.password = "";
        }
      });
    },
    //修改
    xiugai() {
      //修改请求
      requseredit(this.form).then((res) => {
        //修改成功
        if (res.data.code == 200) {
          // 1.弹成功
          successAlert(res.data.msg);
          
          // 3.弹框消失
          this.info.isshow = false;
          
          //判断是否修改的是当前登录的用户
          if(this.form.uid==this.user.uid){
            //清空
            this.changeuser({})
            this.$router.replace("/login")
            return;
          }
          // 2.表单清空
          this.qingkong();
          // 4.列表更新
          this.$emit("init");
        }
      });
    },
    //弹框关闭
    close() {
      // 是编辑，要清空表单
      !this.info.isAdd && this.qingkong();
    },
  },
};
</script>
<style scoped lang="less">
@import "../../../less/index.less";
</style>